package com.example.iesb3;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.telephony.CellIdentityGsm;
import android.telephony.CellIdentityLte;
import android.telephony.CellIdentityWcdma;
import android.telephony.CellInfo;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoWcdma;
import android.telephony.CellSignalStrengthGsm;
import android.telephony.CellSignalStrengthLte;
import android.telephony.CellSignalStrengthWcdma;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	CellInfoLte cellInfoLte;
	CellSignalStrengthLte cellSignalStrengthLte;
	CellSignalStrengthGsm cellSignalStrengthGsm;
	CellIdentityLte cellIdLte;
	CellIdentityGsm cellIdGsm;
	
	private TextView id1,id2,id3;
	private TextView tvpw1,tvpw2,tvpw3, tvTec;
	private TextView tvlat, tvlong;
	private boolean stpr, gpsEnable;
	private TelephonyManager tm;
	private GPSTracker gpsT;


	@SuppressWarnings("static-access")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setup();
		
		stpr = false;
		gpsEnable = false;
		
		id1 = new TextView(this);
		id2 = new TextView(this);
		id3 = new TextView(this);		
		
		tvpw1 = new TextView(this);
		tvpw2 = new TextView(this);
		tvpw3 = new TextView(this);
		tvTec = new TextView(this);
		
		tvlat = new TextView(this);
		tvlong = new TextView(this);
		
		id1 = (TextView) findViewById(R.id.textViewId);
		id2 = (TextView) findViewById(R.id.textViewStr);
		id3 = (TextView) findViewById(R.id.tvID3);
		
		tvpw1 = (TextView) findViewById(R.id.tvPw1);
		tvpw2 = (TextView) findViewById(R.id.tvpw2);
		tvpw3 = (TextView) findViewById(R.id.tvPw3);
		tvTec = (TextView) findViewById(R.id.tvTec);
		
		tvlat = (TextView) findViewById(R.id.tvLat);
		tvlong = (TextView) findViewById(R.id.tvLong);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		tm = (TelephonyManager) this.getSystemService(this.TELEPHONY_SERVICE);
		
		adquirirDados(gpsT.getLocation());
		cellInfoLte.getCellIdentity().getCi();
//		id.setText(new String().valueOf(cellInfoLte.getCellIdentity().getCi()));
//		str.setText(new String().valueOf(cellSignalStrength.getmRsrp()));
	}
	
	public void adquirirDados(View view){
		adquirirDados(gpsT.getLocation());
	}

	private void adquirirDados(Location location) {
		if(location != null){
			location.getLongitude();
			
			tvlat.setText(Double.toString(location.getLatitude()));
			tvlong.setText(Double.toString(location.getLongitude()));
		}
		if(tm.getAllCellInfo() == null){
			id1.setText("null");
			id2.setText("null");
			id3.setText("null");
			
			tvpw1.setText("null");
			tvpw2.setText("null");
			tvpw3.setText("null");
		}else{
			for (CellInfo cellInfo : tm.getAllCellInfo())
			{
			    if (cellInfo instanceof CellInfoLte)
			    {
			        // cast to CellInfoLte and call all the CellInfoLte methods you need
			    	cellIdLte = ((CellInfoLte) cellInfo).getCellIdentity();
			    	cellSignalStrengthLte = ((CellInfoLte) cellInfo).getCellSignalStrength();
			    	
			    	tvTec.setText("LTE");
			    	
			    	id1.setText(Integer.toString(cellIdLte.getPci()));
			    	id2.setText(Integer.toString(cellIdLte.getTac()));
			    	id3.setText(Integer.toString(cellIdLte.getCi()));
			    	
			    	tvpw1.setText(Integer.toString(cellSignalStrengthLte.getAsuLevel()));
//			    	tvpw2.setText(Integer.toString(cellSignalStrengthLte.getmRsrp()));
			    	tvpw3.setText(Integer.toString(cellSignalStrengthLte.getDbm()));
			    }
			    else if(cellInfo instanceof CellInfoGsm){
			    	cellIdGsm = ((CellInfoGsm) cellInfo).getCellIdentity();
			    	cellSignalStrengthGsm = ((CellInfoGsm) cellInfo).getCellSignalStrength();
			    	
			    	tvTec.setText("WCDMA");
			    	
//			    	id1.setText(Integer.toString(cellIdGsm.get)));
			    	id2.setText(Integer.toString(cellIdGsm.getLac()));
			    	id3.setText(Integer.toString(cellIdGsm.getCid()));
			    	
			    	tvpw1.setText(Integer.toString(cellSignalStrengthGsm.getDbm()));
			    	tvpw2.setText(Integer.toString(cellSignalStrengthGsm.getAsuLevel()));
			    	tvpw3.setText(Integer.toString(cellSignalStrengthGsm.getLevel()));
			    	
			    }
			}
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private void setup(){
		cellInfoLte = new CellInfoLte();
		cellSignalStrengthLte = new CellSignalStrengthLte();
		
		gpsT=  new GPSTracker(this);
		
		if(gpsT.canGetLocation){
			
			gpsEnable=true;
			
		}else{
			gpsT.showSettingsAlert();
		}
	}
	

}
